package com.cssnj.portal.web.manage.modify;

import com.cssnj.portal.domain.func.GlGnEntity;
import com.cssnj.portal.service.common.TreeService;
import com.cssnj.portal.service.manage.apply.ApplyService;
import com.cssnj.portal.service.manage.modify.ModifyApplyService;
import com.cssnj.portal.service.manage.release.ReleaseService;
import com.cssnj.portal.utils.PortalUntil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import io.predictech.framework.domain.common.JsonRequest;
import io.predictech.framework.domain.common.JsonResponse;
import io.predictech.framework.domain.constants.SysConstants;
import io.predictech.framework.utility.StringUtil;
import io.predictech.framework.web.common.FileHelper;
import io.predictech.framework.web.common.WebHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 功能变更申请控制器
 *
 * @author Arthur
 * @version 1.0
 * @since 2018-9-18 13:48
 */
@Controller
@RequestMapping(value = "/manage/modify")
public class ModifyApplyController {

    @Autowired
    ApplyService applyService;

    @Autowired
    ModifyApplyService modifyApplyService;

    @Autowired
    private ReleaseService releaseService;

    @Autowired
    private FastFileStorageClient fastFileStorageClient;

    @Autowired
    private TreeService treeService;

    /**
     * @description 跳转应用申请页面
     * @author Arthur
     * @since 2018-9-11 15:57
     **/
    @RequestMapping(value = "/modifyApply.do")
    public ModelAndView modifyApply(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("manage/modify/modifyApply");

        modelAndView.addAllObjects(applyService.getAppMsg().getRepData());

        return modelAndView;
    }

    @ResponseBody
    @RequestMapping(value = "/getFunc.do")
    @SuppressWarnings("unchecked")
    public String getSjFunc(HttpServletRequest request, @ModelAttribute JsonRequest jsonRequest) throws JsonProcessingException {
        jsonRequest.getReqData().put("funcId", StringUtil.emptyToIe(request.getParameter("id")));
        jsonRequest.getReqData().put("needLeaf", "1");

        // 整理功能树list
        return StringUtil
                .getBaseObjectMapper()
                .writeValueAsString(
                        ((List<GlGnEntity>) treeService.getFunc(jsonRequest).getRepData().get("funcList"))
                                .stream()
                                .map(PortalUntil::assFuncTree)
                                .collect(Collectors.toList())
                );
    }

    @ResponseBody
    @RequestMapping(value = "/getFuncDetail.do")
    public JsonResponse getFuncDetail(@ModelAttribute JsonRequest jsonRequest) throws SQLException {
        return modifyApplyService.getApplyByFuncId(jsonRequest);
    }

    @ResponseBody
    @RequestMapping(value = "/establishModifyApply.do")
    public JsonResponse establishApply(HttpServletRequest request, @ModelAttribute JsonRequest jsonRequest) throws IOException, SQLException {
        JsonResponse jsonResponse;
        // 获取所有提交数据
        request.getCharacterEncoding();
        jsonRequest.getReqData().putAll(WebHelper.getParamFromRequest(request));
        // 生成应用变更申请
        jsonResponse = modifyApplyService.establishModifyApply(jsonRequest);
        if (!SysConstants.SUCESS_CODE.equals(jsonResponse.getCode())) {
            return jsonResponse;
        }
        // 上传文件
        jsonRequest.getReqData().put("attachList", FileHelper.upLoad(request, fastFileStorageClient));
        // 保存应用附件路径
        jsonResponse = applyService.saveAttachPath(jsonRequest);
        if (!SysConstants.SUCESS_CODE.equals(jsonResponse.getCode())) {
            return jsonResponse;
        }
        // 发布Func -- 若需要审批流程 此处应在 ReleaseController 调用
        return releaseService.releaseApp(jsonRequest);
    }
}
